package com.chuangyang.fixboxmaster.ui.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.chuangyang.fixboxmaster.AppData;
import com.chuangyang.fixboxmaster.R;
import com.chuangyang.fixboxmaster.bean.CostItem;

import java.util.ArrayList;

/**
 * Created by Administrator on 2015/6/16.
 */
public class RepairTypeListAdapter  extends BaseAdapter {

    private ArrayList<CostItem> mCostItems;

    private CostItem mCurrentSelectItem;

    public RepairTypeListAdapter(ArrayList<CostItem> costItems, CostItem currentSelectItem){
        mCostItems = costItems;
        mCurrentSelectItem = currentSelectItem;
    }

    public void setSelectItem(CostItem currentSelectItem){

        mCurrentSelectItem = currentSelectItem;
    }
    @Override
    public int getCount() {
        return mCostItems == null?0:mCostItems.size();
    }

    @Override
    public CostItem getItem(int position) {
        return mCostItems == null?null:mCostItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;
        if (convertView == null)
        {
            holder = new ViewHolder();

            convertView = LayoutInflater.from(AppData.getContext()).inflate(R.layout.repair_type_list_item, parent, false);
            holder.mSelectImageView = (ImageView)convertView.findViewById(R.id.iv_select);
            holder.mTitle = (TextView)convertView.findViewById(R.id.tv_repair_type_title);
            holder.mPrice = (TextView)convertView.findViewById(R.id.tv_repair_type_price);
            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder)convertView.getTag();
        }

        CostItem item = getItem(position);

        if (item != null)
        {
            if (mCurrentSelectItem != null && mCurrentSelectItem.title != null && mCurrentSelectItem.title.equals(item.title))
            {
                holder.mSelectImageView.setVisibility(View.VISIBLE);
            }
            else
            {
                holder.mSelectImageView.setVisibility(View.INVISIBLE);
            }
            holder.mTitle.setText(item.title);
            holder.mPrice.setText("￥"+item.price);
        }
        return convertView;
    }


    public class ViewHolder{

        public ImageView mSelectImageView;

        public TextView mTitle;

        public TextView mPrice;

    }
}
